<template>
  <el-dialog
    append-to-body
    :before-close="beforeClose"
    :visible.sync="show"
    :title="title"
    width="35%"
    @close="close"
    @closed="closed"
  >
    <el-form ref="myForm" label-width="100px" :model="form" :rules="rules">
      <el-tabs v-model="activeName">
        <el-tab-pane label="基础资料" name="first">
          <el-row>
            <el-col :span="12">
              <el-form-item label="楼宇" prop="building_id">
                <buliding-select v-model="form.building_id" @otherData="handleOtherData" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="企业名称" prop="name">
                <el-input
                  v-model="form.name"
                  type="text"
                />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <!-- <el-col :span="12"> <el-form-item label="房间号">
          <el-input
            v-model="form.item"
            type="text"
          />
        </el-form-item></el-col> -->
            <el-col :span="24">
              <el-form-item label="房间号">
                <RoomMultiSelect
                  v-model="form.room_ids"
                  :building-id="form.building_id"
                  style="width:100%"
                  type="text"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="门牌地址">
                <el-input
                  v-model="form.house_address"
                  type="text"
                />
              </el-form-item></el-col>
            <el-col :span="12">
              <el-form-item label="是否工商注册">
                <el-select v-model="form.is_registered">
                  <el-option :value="1" label="是" />
                  <el-option :value="2" label="否" />
                </el-select>
              </el-form-item></el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="社会统一信用代码" prop="code">
                <el-input
                  v-model="form.code"
                  type="text"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="员工规模">
                <el-input
                  v-model="form.people_number"
                  type="text"
                />
              </el-form-item></el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="公司类型">
                <el-input
                  v-model="form.type"
                  type="text"
                />
              </el-form-item></el-col>
            <el-col :span="12">
              <el-form-item label="所属行业">
                <el-select v-model="form.industry" placeholder="请选择">
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>

              </el-form-item></el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否注册地一致">
                <el-select v-model="form.registered_address_status">
                  <el-option :value="1" label="是" />
                  <el-option :value="2" label="否" />
                </el-select>

              </el-form-item></el-col>
            <el-col :span="12">
              <el-form-item label="行业标签">
                <el-input
                  v-model="form.industry_label"
                  type="text"
                />
              </el-form-item></el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="经营范围">
                <el-input
                  v-model="form.business_scope"
                  type="text"
                />
              </el-form-item></el-col>

          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="注册地址">
                <el-input
                  v-model="form.registered_address"
                  type="text"
                />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="登记机关">
                <el-input
                  v-model="form.registration_authority"
                  type="text"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="核准日期">
                <el-date-picker
                  v-model="form.approve_time"
                  type="date"
                  placeholder="选择日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否为总公司">
                <el-select v-model="form.head_office">
                  <el-option :value="1" label="是" />
                  <el-option :value="2" label="否" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="法人信息" name="second">

          <el-form-item label="法人信息">
            <el-input
              v-model="form.legal_person"
              type="text"
            />
          </el-form-item>

          <el-form-item label="联系电话">
            <el-input
              v-model="form.legal_person_mobile"
              type="text"
            />
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select v-model="form.legal_person_certificate_type">
              <el-option :value="1" label="身份证" />
              <el-option :value="2" label="护照" />
              <el-option :value="3" label="驾照" />
            </el-select>

          </el-form-item>
          <el-form-item label="证件号">
            <el-input
              v-model="form.legal_person_certificate_code"
              type="text"
            />
          </el-form-item>
          <el-form-item label="是否有前科">
            <el-select v-model="form.legal_person_status">
              <el-option :value="2" label="否" />
              <el-option :value="1" label="是" />
            </el-select>
          </el-form-item>
          <el-form-item label="国籍">
            <el-input
              v-model="form.legal_person_nationality"
              type="text"
            />
          </el-form-item>

        </el-tab-pane>
        <el-tab-pane label="实际控制人信息" name="third">
          <el-form-item label="控制人信息">
            <el-input
              v-model="form.control_person"
              type="text"
            />
          </el-form-item>

          <el-form-item label="联系电话">
            <el-input
              v-model="form.control_person_mobile"
              type="text"
            />
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select v-model="form.control_person_certificate_type">
              <el-option :value="1" label="身份证" />
              <el-option :value="2" label="护照" />
              <el-option :value="3" label="驾照" />
            </el-select>

          </el-form-item>
          <el-form-item label="证件号">
            <el-input
              v-model="form.control_person_certificate_code"
              type="text"
            />
          </el-form-item>
          <el-form-item label="是否有前科">
            <el-select v-model="form.control_person_status">
              <el-option :value="2" label="否" />
              <el-option :value="1" label="是" />
            </el-select>
          </el-form-item>
          <el-form-item label="国籍">
            <el-input
              v-model="form.control_person_nationality"
              type="text"
            />
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="物业信息" name="fourth">
          <el-form-item label="房东姓名">
            <el-input
              v-model="form.landlord"
              type="text"
            />
          </el-form-item>

          <el-form-item label="房东电话">
            <el-input
              v-model="form.landlord_mobile"
              type="text"
            />
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select v-model="form.landlord_certificate_type">
              <el-option :value="1" label="身份证" />
              <el-option :value="2" label="护照" />
              <el-option :value="3" label="驾照" />
            </el-select>

          </el-form-item>
          <el-form-item label="证件号">
            <el-input
              v-model="form.landlord_certificate_code"
              type="text"
            />
          </el-form-item>
          <el-form-item label="租赁时间">
            <el-date-picker
              v-model="form.join_time"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="到期时间">
            <el-date-picker
              v-model="form.expire_time"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="水电物业是否正常缴纳">
            <el-select v-model="form.fee_status">
              <el-option :value="1" label="是" />
              <el-option :value="2" label="否" />
            </el-select>
          </el-form-item>

        </el-tab-pane>
      </el-tabs>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmitChek">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ssqOptions, streetOptions, adminOptions, industryOptions } from '@/list/data'
import bulidingSelect from '@/components/Select/BulidingSelect'
import RoomMultiSelect from '@/components/Select/RoomMultiSelect'
import { companyGet, companyUpdate, companyCreate } from '@/api/company'
export default {
  name: 'Index',
  components: { bulidingSelect, RoomMultiSelect },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'view'
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      show: false,
      ssqOptions,
      streetOptions,
      adminOptions,
      industryOptions,
      form: {
        type: 1,
        room_ids: []
      },
      rules: {
        building_id: [{ required: true, message: '请选择楼宇', trigger: 'change' }],
        name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入社会统一信用代码', trigger: 'blur' }]
      },
      activeName: 'first'
    }
  },
  computed: {
    title() {
      let text = ''
      switch (this.type) {
        case 'view':
          text = '查看'
          break
        case 'create':
          text = '新建'
          break
        case 'edit':
          text = '编辑'
          break
      }
      return text
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.show = val
        this.getData()
        this.$nextTick(function() {
          this.$refs['myForm'].clearValidate()
        })
      }
    }
  },
  mounted() {
    this.show = this.visible
  },
  methods: {
    handleChange(value) {
      console.log(value)
    },
    handleOtherData(data) {
      this.form.region_id = data.region_id
    },
    getData() {
      console.log('获取数据')
      if (this.data.id > 0) {
        companyGet(this.data.id).then(res => {
          const room_id = []
          res.data.rooms.forEach(item => {
            room_id.push(item.id)
          })
          res.data.room_ids = room_id
          this.form = res.data
        })
      } else {
        this.form = {

        }
      }
    },
    handleClose() {
      console.log('点击关闭')
      this.beforeClose()
    },
    handleSubmitChek() {
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          this.handleSubmit()
        } else {
          return false
        }
      })
    },
    handleSubmit() {
      console.log('点击确定')
      if (this.form.id > 0) {
        companyUpdate(this.form).then(res => {
          this.$notify.success('保存成功')
          this.handleClose()
        })
      } else {
        companyCreate(this.form).then(res => {
          this.$notify.success('保存成功')
          this.handleClose()
        })
      }
      this.$emit('refresh')
    },
    beforeClose() {
      console.log('关闭前的回调')
      this.show = false
      this.$emit('update:visible', false)
    },
    close() {
      console.log('关闭的回调')
    },
    closed() {
      console.log('关闭动画结束后的回调')
    }
  }
}
</script>

<style scoped>
</style>
